package edu.ielab.localansys.ui;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Dimension;
import javax.swing.JTextArea;
import javax.swing.JLabel;

public class ApdlUI extends JPanel {

	private static final long serialVersionUID = 1L;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JButton jButton = null;
	private Table_Model model = null;
	private JButton jButton1 = null;
	private JLabel jLabel = null;
	private JScrollPane jScrollPane1 = null;
	private JTextArea jTextArea = null;
	
	/**
	 * This is the default constructor
	 */
	public ApdlUI() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(15, 380, 74, 21));
		jLabel.setText("Console");
		this.setSize(688, 513);
		this.setLayout(null);
		this.add(getJScrollPane(), null);
		this.add(getJButton(), null);
		this.add(getJButton1(), null);
		this.add(jLabel, null);
		this.add(getJScrollPane1(), null);
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(33, 40, 607, 339));
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			model = new Table_Model(2000);
			jTable = new JTable(model);
			jTable.setBounds(new Rectangle(0, 0, 700, 0));
		}
		return jTable;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(149, 13, 121, 21));
			jButton.setText("Load APDL File");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					loadAPDLFile(); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return jButton;
	}

	protected void loadAPDLFile() {
		//read file content
		JFileChooser fc=new JFileChooser();
		String str="";
		fc.showOpenDialog(this);
		if(fc.getSelectedFile()==null){
		} else {
			try{
				FileReader fr=new FileReader(fc.getSelectedFile());
				BufferedReader br = new BufferedReader(fr);
				 while((str = br.readLine()) != null) {
					 TripleRowContent trc=  buildAPDLLineToTRC(str);
					 model.addRow(trc);
	            }
	           
	            br.close();
				fr.close();
			}catch(IOException ie){
				JOptionPane.showMessageDialog(this,"I/O Error","error",JOptionPane.ERROR_MESSAGE);
			}
			jTable.updateUI();
		}
			
	}

	private TripleRowContent buildAPDLLineToTRC(String str) {
		//name ,/=(lication1 is ',' or '='), value !(location2 is '!')description
		String name="";
		String value="";
		String description = "";
		int location1 = -1;
		int location2 = -1;
		int strLength = str.length();
		
		//location2
		location2 = str.indexOf("!");
		
		//description
		if(location2 != -1)
			description = str.substring(location2, strLength);

		//location1
		//in case there is '=' or ',' in description
		location1 = str.indexOf("=");
		if(location1 > location2)
			location1 = -1;
		if(location1==-1)
			location1 = str.indexOf(",");
		if(location1 > location2)
			location1 = -1;
		
		
		
		//value
		if(location1 != -1 && location2!=-1) {
			value = str.substring(location1, location2);
		} else if(location1 != -1 && location2==-1) {
			value = str.substring(location1, str.length());
		}
		
		//name
		if(location2==-1 && location1==-1) {
			name = str;
		} else if(location2 == -1) {
			name = str.substring(0, location1);
		} else if(location1 == -1) {
			name = str.substring(0, location2);
		} else {
			name = str.substring(0, location1);
		}
		return new TripleRowContent(name, value, description);
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(385, 12, 122, 23));
			jButton1.setText("Execute");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					executeAPDL();
				}
			});
		}
		return jButton1;
	}

	protected void executeAPDL() {
		System.out.println(model.getRowCount());
		for(int i=0; i<model.getRowCount(); i++) {
			model.getRowWithoutDes(i);
		}
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(15, 400, 651, 105));
			jScrollPane1.setViewportView(getJTextArea());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
		}
		return jTextArea;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
